package com.gtt.lazy.service.impl;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.type.Type;

import com.gtt.lazy.domain.Sofa;
import com.gtt.lazy.service.SofaService;
import com.gtt.lazy.util.HibernateTemplateUtil;

/**
 * 沙发服务实现
 * 
 * @author 高甜甜
 * @date 2009-7-7
 */
public class SofaServiceImpl implements SofaService {

	private static final Logger logger = Logger
			.getLogger(SofaServiceImpl.class);

	private HibernateTemplateUtil hibernateTemplateUtil;

	public int add(Sofa sofa) {
		if (logger.isDebugEnabled())
			logger.debug("add Sofa ");
		return (Integer) hibernateTemplateUtil.save(sofa);
	}

	public void delete(int id) {
		if (logger.isDebugEnabled())
			logger.debug("delete CategoryToSofa [ *:" + id + " ]");
		String hql = "delete from CategoryToSofa as cts where cts.sid=?";
		hibernateTemplateUtil.update(hql, new Object[] { id },
				new Type[] { Hibernate.INTEGER });

		if (logger.isDebugEnabled())
			logger.debug("delete Sofa [ " + id + " ]");
		hibernateTemplateUtil.delete(Sofa.class, id);
	}

	public Sofa get(int id) {
		return (Sofa) hibernateTemplateUtil.get(Sofa.class, id);
	}

	public void update(Sofa sofa) {
		if (logger.isDebugEnabled())
			logger.debug("update Sofa " + sofa.toString());
		hibernateTemplateUtil.update(sofa);
	}

	public void updateDiscount(int id, boolean isDiscount) {
		if (logger.isDebugEnabled())
			logger.debug("update Sofa Discount [id=" + id + " isDiscount="
					+ isDiscount);
		Sofa sofa = get(id);
		sofa.setDiscounted(isDiscount);
		update(sofa);
	}

	public void updateFresh(int id, boolean isFresh) {
		if (logger.isDebugEnabled())
			logger.debug("update Sofa Fresh [id=" + id + " isFresh=" + isFresh);
		Sofa sofa = get(id);
		sofa.setFresh(isFresh);
		update(sofa);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> list() {
		String hql = "from Sofa as s order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> list(int start, int size) {
		String hql = "from Sofa as s order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql, start, size);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listDiscounted() {
		String hql = "from Sofa as s where s.discounted=? order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN });
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listDiscounted(int start, int size) {
		String hql = "from Sofa as s where s.discounted=? order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN }, start, size);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listFresh() {
		String hql = "from Sofa as s where s.fresh=? order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN });
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listFresh(int start, int size) {
		String hql = "from Sofa as s where s.fresh=? order by s.publishDate desc";
		return hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN }, start, size);
	}

	public int count() {
		String hql = "select count(*) from Sofa as s";
		Long l = (Long) hibernateTemplateUtil.find(hql).get(0);
		return l.intValue();
	}

	public int countDiscounted() {
		String hql = "select count(*) from Sofa as s where s.discounted=?";
		Long l = (Long) hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN }).get(0);
		return l.intValue();
	}

	public int countFresh() {
		String hql = "select count(*) from Sofa as s where s.fresh=?";
		Long l = (Long) hibernateTemplateUtil.find(hql, new Object[] { true },
				new Type[] { Hibernate.BOOLEAN }).get(0);
		return l.intValue();
	}

	public void setHibernateTemplateUtil(
			HibernateTemplateUtil hibernateTemplateUtil) {
		this.hibernateTemplateUtil = hibernateTemplateUtil;
	}

}
